package com.stadium.handler;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.stadium.bean.Equipment;
import com.stadium.servlet.EquipmentServlet;
import com.stadium.tools.ResultMessage;

@Controller
@RequestMapping(value = "views/")
public class EquipmentHandler {

	@Autowired
	private EquipmentServlet equipmentServlet;

	// 器材分页信息查询
	// 参数：pageNumStr：第几页数
	@RequestMapping(value = "equipsInfo")
	public ResponseEntity<ResultMessage> equipsInfo(
			@RequestParam(name = "pageNumStr", required = false, defaultValue = "1") String pageNumStr) {
		// 类型转换
		int pageNum = 1;
		try {
			pageNum = Integer.valueOf(pageNumStr);
			if (pageNum < 1) {
				pageNum = 1;
			}
		} catch (Exception e) {
			pageNum = 1;
			System.out.println("pageNumStr数据类型出错");
		}
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("page",
				equipmentServlet.getPages(pageNum, 10));
		result.setResultInfo("查询成功");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	
	//获取全部器材信息
	@RequestMapping(value = "getEquipsInfo")
	public ResponseEntity<ResultMessage> getEquipsInfo() {

		ResultMessage result = new ResultMessage();
		result.getResultParm().put("equips", equipmentServlet.getAll());
		result.setResultInfo("查询成功");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}
	

	// 根据器材名称查询对应信息
	@RequestMapping(value = "getEquipName")
	public ResponseEntity<ResultMessage> getEquip(
			@RequestParam(name = "equipName") String equipName) {
		
		System.out.println("equipName:" + equipName);
		ResultMessage result = new ResultMessage();
		result.getResultParm().put("equip", equipmentServlet.getByName(equipName));
		result.setResultInfo("查询成功");
		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 插入器材信息
	@RequiresRoles(value = "equipManager")
	@RequestMapping(value = "insertEquip")
	public ResponseEntity<ResultMessage> insertEquip(
			@RequestParam(name = "equipName") String equipName,
			@RequestParam(name = "number") Integer number) {

		Equipment equipment = new Equipment(null, equipName,0, number);
		equipmentServlet.save(equipment);

		ResultMessage result = new ResultMessage();
		result.setResultInfo("插入成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

	// 更新器材信息
	@RequiresRoles(value = "equipManager")
	@RequestMapping(value = "beforeUpdateEquip")
	public ResponseEntity<ResultMessage> updateEquip(
			@RequestParam(name = "equipName") String equipName) {
		Equipment equipment = equipmentServlet.getByName(equipName);

		ResultMessage result = new ResultMessage();
		result.getResultParm().put("equipment", equipment);
		result.setResultInfo("查询成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);

	}

	@RequiresRoles(value = "equipManager")
	@RequestMapping(value = "updateEquip")
	public ResponseEntity<ResultMessage> updateEquip(Equipment equipment) {
		Equipment equip = equipmentServlet.getById(equipment.getId());
		equipment.setStatus(equip.getStatus());
		equipmentServlet.save(equipment);

		ResultMessage result = new ResultMessage();
		result.getResultParm().put("equipment", equipment);
		result.setResultInfo("更新成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);

	}

	// 删除器材，把器材的状态status值改为1
	@RequiresRoles(value = {"equipManager"})
	@RequestMapping(value = "deleteEquip")
	public ResponseEntity<ResultMessage> deleteEquip(
			@RequestParam(name = "id") Integer id) {
		Equipment equipment = equipmentServlet.getById(id);
		equipment.setStatus(1);
		equipmentServlet.save(equipment);

		ResultMessage result = new ResultMessage();
		result.setResultInfo("删除成功!");

		return new ResponseEntity<ResultMessage>(result, HttpStatus.OK);
	}

}
